import * as cheerio from "cheerio";
import dayjs from "dayjs";

export const htmlToJson = (html) => {
    const $ = cheerio.load(html);
    const wechatAccount = {
        name: $('.gzh-tit em').text().trim(),
        wechat_id: $('.gzh-name').text().trim().split('：')[1],
        image_url: $('.img-box img').attr('src'),
        // verification: {
        //     organization: $('dd').eq(2).text().trim()
        // }
    };

    const recentArticle = {
        title: $('dl dd a').text().trim(),
        timestamp: $('script').eq(1).text().trim().match(/timeConvert\('(\d+)'\)/)[1],
        //published_time: $('dl dd span').text().trim(),
        url: $('dl dd a').attr('href')
    };

    return {
        //description: $('dl dd').eq(0).text().trim(),
        name: wechatAccount.name,
        wechat_id:wechatAccount.wechat_id,
        image_url:wechatAccount.image_url,
        timestamp: recentArticle.timestamp,
        releaseTime: dayjs(recentArticle.timestamp*1000).format('YYYY-MM-DD HH:mm:ss'),
        url: recentArticle.url
    };
}